package com.huaxonline.boot.web.sign;

import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * 签名功能使用示例
 * @author zhaoshulei
 * @since 2023/9/13
 */
@Slf4j
public class SignExample {
    
    public static void main(String[] args) {
        // 示例1: 基本使用
        basicExample();
        
        // 示例2: 使用SDK客户端
        sdkClientExample();
        
        // 示例3: 快速签名
        quickSignExample();
        
        // 示例4: 发送HTTP请求
        httpRequestExample();
    }
    
    /**
     * 基本使用示例
     */
    public static void basicExample() {
        log.info("=== 基本使用示例 ===");
        
        String appId = "test_app";
        String secret = "test_secret";
        
        // 创建签名客户端
        SignClient client = new SignClient(appId, secret);
        client.setDebugMode(true); // 启用调试模式
        
        // 生成签名参数
        SignClient.SignParams signParams = client.generateSignParams();
        log.info("AppId: {}", signParams.getAppId());
        log.info("Timestamp: {}", signParams.getTimestamp());
        log.info("Nonce: {}", signParams.getNonce());
        
        // 构建参数
        Map<String, Object> params = new HashMap<>();
        params.put("name", "张三");
        params.put("age", 25);
        params.put(SignConstants.HEADER_TIMESTAMP, signParams.getTimestamp());
        params.put(SignConstants.HEADER_NONCE, signParams.getNonce());
        
        // 生成签名
        String signature = client.generateSign(params);
        log.info("签名: {}", signature);
    }
    
    /**
     * SDK客户端示例
     */
    public static void sdkClientExample() {
        log.info("=== SDK客户端示例 ===");
        
        String appId = "test_app";
        String secret = "test_secret";
        
        // 创建客户端（指定算法）
        SignClient client = new SignClient(appId, secret, SignAlgorithm.HMAC_SHA256);
        client.setDebugMode(true);
        
        // 构建参数
        Map<String, Object> params = new HashMap<>();
        params.put("userId", "12345");
        params.put("action", "query");
        
        // 发送GET请求
        try {
            HttpResponse response = client.sendGet("http://localhost:8080/api/user", params);
            log.info("GET响应状态: {}", response.getStatus());
            log.info("GET响应内容: {}", response.body());
        } catch (Exception e) {
            log.error("GET请求失败", e);
        }
        
        // 发送POST请求
        try {
            Map<String, Object> postData = new HashMap<>();
            postData.put("name", "李四");
            postData.put("email", "lisi@example.com");
            
            HttpResponse response = client.sendPostJson("http://localhost:8080/api/user", postData);
            log.info("POST响应状态: {}", response.getStatus());
            log.info("POST响应内容: {}", response.body());
        } catch (Exception e) {
            log.error("POST请求失败", e);
        }
    }
    
    /**
     * 快速签名示例
     */
    public static void quickSignExample() {
        log.info("=== 快速签名示例 ===");
        
        String appId = "test_app";
        String secret = "test_secret";
        
        // 构建参数
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", "ORDER123456");
        params.put("amount", 100.50);
        
        // 快速生成签名
        String signature = SignUtil.quickSign(appId, secret, params);
        log.info("快速签名: {}", signature);
        
        // 快速生成签名（包含请求体）
        String jsonBody = "{\"productId\":\"PROD001\",\"quantity\":2}";
        String signatureWithBody = SignUtil.quickSignWithBody(appId, secret, params, jsonBody);
        log.info("包含请求体的签名: {}", signatureWithBody);
    }
    
    /**
     * HTTP请求示例
     */
    public static void httpRequestExample() {
        log.info("=== HTTP请求示例 ===");
        
        String appId = "test_app";
        String secret = "test_secret";
        
        SignClient client = new SignClient(appId, secret);
        client.setDebugMode(true);
        
        // 示例1: 查询用户信息
        Map<String, Object> queryParams = new HashMap<>();
        queryParams.put("userId", "12345");
        
        try {
            HttpResponse response = client.sendGet("http://localhost:8080/api/user/info", queryParams);
            log.info("查询用户信息响应: {}", response.body());
        } catch (Exception e) {
            log.error("查询用户信息失败", e);
        }
        
        // 示例2: 创建订单
        Map<String, Object> orderData = new HashMap<>();
        orderData.put("productId", "PROD001");
        orderData.put("quantity", 2);
        orderData.put("price", 99.99);
        
        try {
            HttpResponse response = client.sendPostJson("http://localhost:8080/api/order/create", orderData);
            log.info("创建订单响应: {}", response.body());
        } catch (Exception e) {
            log.error("创建订单失败", e);
        }
        
        // 示例3: 上传文件
        Map<String, Object> uploadParams = new HashMap<>();
        uploadParams.put("fileName", "test.txt");
        uploadParams.put("fileSize", 1024);
        
        String fileContent = "这是测试文件内容";
        
        try {
            HttpResponse response = client.sendPost("http://localhost:8080/api/file/upload", uploadParams, fileContent);
            log.info("上传文件响应: {}", response.body());
        } catch (Exception e) {
            log.error("上传文件失败", e);
        }
    }
}